import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

const Home1 = () => {
  return <div>home1页面</div>;
};
const Home2 = () => {
  return <div>home2页面</div>;
};

class Home extends Component {
  render() {
    // console.log(this.props);
    const { path } = this.props.match;
    return (
      <>
        <ul>
          <li>
            <Link to={`${path}/home1`}>home1</Link>
          </li>
          <li>
            <Link to={`${path}/home2`}>home2</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path={`${path}/home1`} component={Home1}></Route>
          <Route path={`${path}/home2`} component={Home2}></Route>
        </Switch>
      </>
    );
  }
}

class About extends Component {
  render() {
    return <div>about页面</div>;
  }
}

class Mine extends Component {
  render() {
    return <div>mine页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h3>react-router-嵌套路由</h3>
        <ul>
          <li>
            <Link to="/home">home</Link>
          </li>
          <li>
            <Link to="/about">about</Link>
          </li>
          <li>
            <Link to="/mine">mine</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          {/* 如果一个路由要做二级路由，那么它的一级路由不能加精准匹配 */}
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About} exact></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
